﻿using System.Collections.Generic;
using System.Linq;
using ZilLion.Service.Core.DapperWrapper;
using ZilLion.Service.Core.DapperWrapper.SqlServer;
using ZilLion.Service.WeChatMall.Domain.Entitys.ShopInfo;
using ZilLion.Service.WeChatMall.Domain.Repositorys.Interface.ShopInfo;

namespace ZilLion.Service.WeChatMall.Domain.Repositorys.ShopInfo
{
    public class ShopExtraInfoReponsitoy : SqlServerRespository<ShopExtraInfoEntity>, IShopExtraInfoReponsitory
    {
        public ShopExtraInfoReponsitoy()
        {
            Context = new WechatMallContext();
        }
        public ShopExtraInfoEntity GetShopExtraInfoById(long shopid)
        {
            var querysql = DapperHelper.CreateColSqlByMap<ShopExtraInfoEntity>();
            var sql = $@"SELECT {querysql} FROM shopextrainfo 
            WHERE shopid=@shopid";
            var result = GetList(sql, new {shopid}).FirstOrDefault();
            return result;
        }

        public IEnumerable<ShopExtraInfoEntity> GetShopExtraInfoByappId(string appId)
        {
            var querysql = DapperHelper.CreateColSqlByMap<ShopExtraInfoEntity>("a");
            var sql = $@"SELECT {querysql} FROM shopextrainfo a
            INNER JOIN appshoprelationship b ON a.shopid=b.shopid
            WHERE b.appid=@appid";
            var result = GetList(sql, new { appId });
            return result;
        }

        public void AddInfo(ShopExtraInfoEntity entity, UnitOfWork unit = null)
        {
            Add(entity, unit);
        }

        public void RemoveInfo(ShopExtraInfoEntity entity, UnitOfWork unit = null)
        {
            Remove(entity, unit);
        }

        public void RemoveInfoByShopid(long shopid, UnitOfWork unit = null)
        {
            DapperHelper.Execute("DELETE FROM shopextrainfo WHERE shopid=@shopid", Context, unit, new { shopid });
        }

        public void EditInfo(ShopExtraInfoEntity entity, UnitOfWork unit = null)
        {
            Modify(entity, unit);
        }
    }
}